class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle)
    {
        int m = triangle.size();
        int n = triangle[m - 1].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        dp[0][0] = triangle[0][0];
        for (int i = 1; i < m; i++)
        {
            dp[i][0] += dp[i - 1][0] + triangle[i][0];
            dp[i][triangle[i].size() - 1] += dp[i - 1][triangle[i - 1].size() - 1] + triangle[i][triangle[i].size() - 1];
        }

        for (int i = 2; i < m; i++)
        {
            for (int j = 1; j < triangle[i].size() - 1; j++)
            {
                dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle[i][j];
            }
        }

        return *min_element(dp[m - 1].begin(), dp[m - 1].end());
    }
};